/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.service.impl;

import me.zhengjie.pojo.sqlServer.PPTimeConfig;
import me.zhengjie.utils.ValidationUtil;
import me.zhengjie.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.zhengjie.repository.PPTimeConfigRepository;
import me.zhengjie.service.PPTimeConfigService;
import me.zhengjie.service.dto.PPTimeConfigDto;
import me.zhengjie.service.dto.PPTimeConfigQueryCriteria;
import me.zhengjie.service.mapper.PPTimeConfigMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.zhengjie.utils.PageUtil;
import me.zhengjie.utils.QueryHelp;
import java.util.List;
import java.util.Map;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import me.zhengjie.utils.PageResult;

/**
* @website https://eladmin.vip
* @description 服务实现
* @author xinglin
* @date 2024-08-14
**/
@Service
@RequiredArgsConstructor
public class PPTimeConfigServiceImpl implements PPTimeConfigService {

    private final PPTimeConfigRepository ppTimeConfigRepository;
    private final PPTimeConfigMapper ppTimeConfigMapper;

    @Override
    public PageResult<PPTimeConfigDto> queryAll(PPTimeConfigQueryCriteria criteria, Pageable pageable){
        Page<PPTimeConfig> page = ppTimeConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(ppTimeConfigMapper::toDto));
    }

    @Override
    public List<PPTimeConfigDto> queryAll(PPTimeConfigQueryCriteria criteria){
        return ppTimeConfigMapper.toDto(ppTimeConfigRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public PPTimeConfigDto findById(Integer id) {
        PPTimeConfig ppTimeConfig = ppTimeConfigRepository.findById(id).orElseGet(PPTimeConfig::new);
        ValidationUtil.isNull(ppTimeConfig.getId(),"PpTimeConfig","id",id);
        return ppTimeConfigMapper.toDto(ppTimeConfig);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void create(PPTimeConfig resources) {
        ppTimeConfigRepository.save(resources);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(PPTimeConfig resources) {
        PPTimeConfig ppTimeConfig = ppTimeConfigRepository.findById(resources.getId()).orElseGet(PPTimeConfig::new);
        ValidationUtil.isNull( ppTimeConfig.getId(),"PpTimeConfig","id",resources.getId());
        ppTimeConfig.copy(resources);
        ppTimeConfigRepository.save(ppTimeConfig);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            ppTimeConfigRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<PPTimeConfigDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (PPTimeConfigDto ppTimeConfig : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("预警时间/h", ppTimeConfig.getWarningTime());
            map.put("报警时间/h", ppTimeConfig.getAlarmTime());
            map.put("pp批次号", ppTimeConfig.getPpBatch());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }

    @Override
    public void deleteByPpBatch(String ppBatch) {
        ppTimeConfigRepository.deleteByPpBatch(ppBatch);
    }
}